﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace BanSupport
{

    public class GameLogic
    {

        private static GameLogic _Instance = null;
        public static GameLogic Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new GameLogic();
                }
                return _Instance;
            }
        }

        public GameData gameData;
        public Fix64 deltaTime;
        public Action<string> printAction;
        public bool logEnable = true;
        public int globalId = 0;

        public void Init(Action<string> printAction,bool logEnable)
        {
            this.globalId = 0;
            this.gameData = new GameData();
            this.deltaTime = (Fix64)0.1;
            this.printAction = printAction;
            this.logEnable = logEnable;
            Log("Inited");

            //这里临时增加一些单位
            for (int i = 0; i < 1;i++){
                var liveObject = new LiveObject("Soldier", "Cube", (Fix64)10, (Fix64)1, (Fix64)2, 1);
                gameData.allObjects.Add(liveObject);
            }

        }

        public void Update()
        {
            Log("Update Start");

            for (int i = gameData.allObjects.Count - 1; i >= 0; i--)
            {
                gameData.allObjects[i].Update();
            }

            for (int i = gameData.allObjects.Count - 1; i >= 0; i--)
            {
                if(gameData.allObjects[i].destroy)
                {
                    gameData.allObjects.RemoveAt(i);
                }
            }

            Log("Update End");

        }

        public void Log(string str)
        {
            if (logEnable)
            {
                printAction("GameLogic Log:" + str);
            }
        }

    }
}